package com.gkj.controller;

import com.gkj.blog.business.consts.SessionConst;
import com.gkj.blog.business.service.ArticleArchivesService;
import com.gkj.blog.business.service.UserService;
import com.gkj.blog.business.vo.entity.User;
import com.gkj.blog.framework.holder.RequestHolder;
import com.gkj.blog.framework.object.ResponseVO;
import com.gkj.blog.persistence.po.ArticleArchives;
import com.gkj.blog.util.ResultUtil;
import com.gkj.blog.util.SessionUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/home")
public class HomeInfoController {
    @Autowired
    ArticleArchivesService articleArchivesService;
    @Autowired
    UserService userService;

    @PostMapping("/getArchives")
    public ResponseVO getArchives(Integer size){
        List<ArticleArchives> articleArchives = articleArchivesService.listArticleArchives(size);
        return ResultUtil.success("",articleArchives);
    }
    /**
     * 获取自己的用户信息
     */
    @PostMapping("/getSelfInfo")
    public ResponseVO getUserInfo(){
        User user = SessionUtil.getUser();
        if(user!=null) {
            return ResultUtil.success("", user);
        }else{
            return ResultUtil.error("获取用户信息失败");
        }
    }

    @PostMapping("/editSelfInfo")
    public ResponseVO editSelfInfo(User user){
        User loginUser = SessionUtil.getUser();
        if(loginUser!=null) {
            loginUser.setAvatar(user.getAvatar());
            loginUser.setEmail(user.getEmail());
            loginUser.setQq(user.getQq());
            loginUser.setSex(user.getSex());
            loginUser.setNickname(user.getNickname());
            userService.update(loginUser);
            //提交的图片，不予删除
            List<String> editImages= (List<String>) RequestHolder.getSession().getAttribute(SessionConst.EDIT_IMAGES);
            for(String e:editImages){
                if(user.getAvatar().indexOf(e)!=-1){
                    editImages.remove(e);
                }
            }
            return ResultUtil.success("更改信息成功");
        }else{
            return ResultUtil.error("获取用户信息失败");
        }
    }

    @PostMapping("/updatePassword")
    public ResponseVO updatePassword(String oldPassword,String newPassword){
        return ResultUtil.success("");
    }
    @PostMapping("/heart")
    public ResponseVO heart(){
        return ResultUtil.success("");
    }
}
